//
// Created by User on 24-10-3.
//

#ifndef GENERAL_DECODER_H
#define GENERAL_DECODER_H

#include "media_frame.h"
#include "packet_provider.hpp"
#include "decoder_error.hpp"
#include <queue>

namespace utils
{
    class media_frame;
}

extern "C" {
struct AVFrame;
struct AVCodec;
struct AVCodecContext;
struct AVRational;
struct AVCodecParameters;
}

namespace decoder
{
    class general_decoder
    {
    public:
        explicit general_decoder(interfaces::packet_provider& packet_provider);

        virtual ~general_decoder();

        utils::media_frame get_frame();

        const AVCodecParameters* get_codec() const;

        AVRational get_timebase() const;

        int total_frame() const;

    private:
        void init_decoder();

        void clean_decoder(error::decoder_error::type _type);

        void run_decode();

    protected:
        virtual const AVCodec* get_decoder();

    private:
        interfaces::packet_provider& m_provider;
        const AVCodec* m_codec{};
        AVCodecContext* m_code_ctx{};
        mutable AVCodecParameters* m_codec_p{};
        AVFrame* m_frame{};
        std::queue<utils::media_frame> m_que;
        bool is_end{false};
    };
} // decoder

#endif //GENERAL_DECODER_H
